const Comment = require('../models/comments')
const Answer = require('../models/answers')

class CommentCtl {
  /**
   * 获取某个用户的问题的答案的评论
   * @param {Object} ctx 
   */
  async find(ctx) {
    const { per_page = 3 } = ctx.query
    const page = Math.max(ctx.query.page * 1, 1) - 1
    const perPage = Math.max(per_page * 1, 1)
    const q = new RegExp(ctx.query.q)
    const { questionId, answerId } = ctx.params
    const { rootCommentId } = ctx.query
    ctx.body = await Comment
      .find({ content: q, questionId, answerId, rootCommentId })
      .limit(perPage).skip(page * perPage)
      .populate('commentator replyTo')
  }
  /**
   * 检查评论是否存在
   * @param {Object} ctx 
   * @param {Object} next 
   */
  async checkCommentExist(ctx, next) {
    const comment = await Comment.findById(ctx.params.id).select('+commentator')
    if (!comment) {
      ctx.throw(404, '评论不存在')
    }
    if (ctx.params.questionId && comment.questionId.toString() !== ctx.params.questionId) {
      ctx.throw(404, '该问题下没有此评论')
    }
    if (ctx.params.answerId && comment.answerId.toString() !== ctx.params.answerId) {
      ctx.throw(404, '该答案下没有此评论')
    }
    ctx.state.comment = comment
    await next()
  }
  /**
   * 获取某个特定的评论
   * @param {Object} ctx 
   */
  async findById(ctx) {
    const { filelds = '' } = ctx.query
    const selectFields = filelds.split(';').filter(f => f).map(f => ' +' + f).join('')
    const comment = await Answer.findById(ctx.params.id).select(selectFields).populate('commentator')
    ctx.body = comment
  }
  /**
   * 创建评论
   * @param {Object} ctx 
   */
  async create(ctx) {
    ctx.verifyParams({
      content: { type: 'string', required: true },
      rootCommentId: { type: 'string', required: false },
      replyTo: { type: 'string', required: false }
    })
    const commentator = ctx.state.user._id
    const { questionId, answerId } = ctx.params
    console.log(questionId, answerId)
    const comment = await new Comment({ ...ctx.request.body, commentator, questionId, answerId }).save()
    ctx.body = comment
  }
  /**
   * 检查该用户是否是该评论的创建者
   * @param {Object} ctx 
   * @param {Object} next 
   */
  async checkCommentator(ctx, next) {
    const { comment } = ctx.state
    if (comment.commentator.toString() !== ctx.state.user._id) {
      ctx.body = '没有更改权限'
    }
    await next()
  }
  /**
   * 修改评论信息
   * @param {Object} ctx 
   */
  async update(ctx) {
    ctx.verifyParams({
      content: { type: 'string', required: false }
    })
    const { content } = ctx.request.body
    await ctx.state.comment.update({ content })
    ctx.body = ctx.state.comment
  }
  /**
   * 删除评论
   * @param {Object} ctx 
   */
  async delete(ctx) {
    await Comment.findByIdAndRemove(ctx.params.id)
    ctx.status = 204
  }
}

module.exports = new CommentCtl()